import UIKit
import Moya

public enum Imgur {
  // 1
  static private let clientId = "YOUR CLIENT ID"

  // 2
  case upload(UIImage)
  case delete(String)
}


extension Imgur:TargetType{
    // 1
    public var baseURL: URL {
      return URL(string: "https://api.imgur.com/3")!
    }

    // 2
    public var path: String {
      switch self {
      case .upload: return "/image"
      case .delete(let deletehash): return "/image/\(deletehash)"
      }
    }

    // 3
    public var method: Moya.Method {
      switch self {
      case .upload: return .post
      case .delete: return .delete
      }
    }

    // 4
    public var sampleData: Data {
      return Data()
    }

    // 5
    public var task: Task {
      switch self {
      case .upload(let image):
        let imageData = image.jpegData(compressionQuality: 1.0)!

        return .uploadMultipart([MultipartFormData(provider: .data(imageData),
                                                   name: "image",
                                                   fileName: "card.jpg",
                                                   mimeType: "image/jpg")])
      case .delete:
        return .requestPlain
      }
    }

    // 6
    public var headers: [String: String]? {
      return [
        "Authorization": "Client-ID \(Imgur.clientId)",
        "Content-Type": "application/json"
      ]
    }

    // 7
    public var validationType: ValidationType {
      return .successCodes
    }

}
